--[[
Copyright (C) 2018-2021 The AGYS Windmill Open Source Project

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
]]

local _M={}
local insert=table.insert
local isAutoDark=luajava.bindClass"android.os.Build$VERSION"
.SDK_INT>=29

local function dump(o)
local t={}
--local _t={}
--local _n={}
local space,deep='  ',2
--local function _ToString(o,_k)
--local ty=type(o)
--[[if ty==('number') then
insert(t,o)
elseif ty==('string') then
insert(t,o--[[('%q'):format(o)] ])
elseif ty==('table') then]]
--[[local mt=getmetatable(o)
if mt&&mt.__tostring then
insert(t,tostring(o))
else]]
--deep=deep+2
insert(t,'{')
for k,v in pairs(o) do
--[[if v==_G then
insert(t,('\r\n%s%s\t=%s ;'):format(space:rep(deep-1),k,"_G"))
elseif v!=package.loaded then]]
--if tonumber(k) then
--k=('[%s]'):format(k)
--else
--k=('[%s]'):format(k)
--end
insert(t,('\r\n%s'):format(space:rep(deep-1)))
if v==nil then
---insert(t,'nil ;')
else
insert(t,('"%s",'):format(v))
end
--end
end
insert(t,'\r\n}')
--deep=deep-2
--end
--[[else
insert(t,tostring(o))
end]]
--insert(t," ;")
--return t
--end

--t=_ToString(o,'')
return table.concat(t)
end

local function dumpHistory(o)
local t={}
local _t={}
--local _n={}
local space,deep='  ',0
local function _ToString(o,_k)
--[[if type(o)==('number') then
insert(t,o)
else]]if type(o)==('string') then
insert(t,('%q'):format(o))
else--[[if type(o)==('table') then
local mt=getmetatable(o)
if mt&&mt.__tostring then
insert(t,tostring(o))
else]]
deep=deep+2
insert(t,'{')
for k,v in pairs(o) do
if v==_G then
insert(t,('\r\n%s%s\t=%s ;'):format(space:rep(deep-1),k,"_G"))
elseif v!=package.loaded then
if tonumber(k) then
k=('[%s]'):format(k)
else
k=('[\"%s\"]'):format(k)
end
insert(t,('\r\n%s%s\t= '):format(space:rep(deep-1),k))
--[[if v==nil then
insert(t,'nil ;')
else]]if type(v)==('table') then
if _t[tostring(v)]==nil then
_t[tostring(v)]=v
local _k =("%s%s"):format(_k,k)
_t[tostring(v)]=_k
_ToString(v,_k)
else
insert(t,tostring(_t[tostring(v)]))
insert(t,';')
end
else
_ToString(v,_k)
end
end
end
insert(t,('\r\n%s}'):format(space:rep(deep-1)))
deep=deep-2
end
--[[else
insert(t,tostring(o))
end]]
insert(t," ;")
--return t
end

_ToString(o,"")
return table.concat(t)
end

local function Str2Table(i)
local f,e=pcall(load(("return %s"):format(i)))
return type(e)=="table"&&e||{}
end

----------------

function _M.new(self,context)
local sp=context.getSharedPreferences("widget_oneword",0)
ins=setmetatable({
sp=sp,
se=sp.edit()
},{__index=_M})
return ins
end

function _M.getLayoutRes(type)
--local types={0x7f080000,0x7f080001,0x7f080002}
--return types[math.random(1,#types)]
if type==2 then
return 0x7f070003
elseif isAutoDark||type==0 then
return 0x7f070001
else--if type==1 then
return 0x7f070002
end
end

---------------

function _M.getHistory(self)
local i=self.sp.getString("history","")
return i==""&&{}
||Str2Table(i)
end

function _M.putHistory(self,h)
--print("putHistory",dumpHistory(h))
self.se.putString("history",dumpHistory(h))
.apply()
end

---------------

function _M.getAllConfig(self)
return self.sp.getAll()
end

function _M.removeConfig(self,c)
self.se.remove(c)
.apply()
end

function _M.clearAllConfig(self)
self.se.clear()
.apply()
end

----------------

--[[function _M.setIsNew(self,id,new)
if new then
self.se.putBoolean(id.."_new",true)
else
self.se.remove(id.."_new")
end
self.se.apply()
end

function _M.isNew(self,id)
if id==0 then
return false
end
return self.sp.getBoolean(id.."_new",false)
||!self:containsId(nil,id)
end]]

-----------------

function _M.getIds(self)
local i=self.sp.getString("ids","")
return i==""&&{}
||Str2Table(i)
end

function _M.putIds(self,ids)
--print("putIds",ids[1],type(ids[1]),dump(ids))
self.se.putString("ids",dump(ids))
.commit()
end

local function mGetNewIds(self)
local i=self.sp.getString("new_ids","")
return i==""&&{}
||Str2Table(i)
end

local function mPutNewIds(self,ids)
self.se.putString("new_ids",dump(ids))
.commit()
end

function _M.putNewIds(self,ids)
local i=mGetNewIds(self)
for _,v in ipairs(ids) do
table.insert(i,v)
end
mPutNewIds(self,i)
end

function _M.isNewId(self,id)
local i=mGetNewIds(self)
--local oi=_M.getIds(self)
--print(dump(i))
local new=table.find(i,id)!=nil
--local find=table.find(oi,id)==nil
if new then
for k,v in ipairs(i) do
if v==id then
table.remove(i,k)
break
end
end
mPutNewIds(self,i)
--table.insert(oi,id)
--_M.putIds(self,oi)
end
return new--||find
end

--[[function _M.containsId(self,data,id)
if id==0 then
return false
end
if data==nil then
data=self:getIds()
end
--print(dump(data),id)
return table.find(data,tostring(id))!=nil
end]]

---------------

function _M.getClickAction(self,id)
if id==0 then
return ""
end
return self.sp.getString(id.."_click","detail")
end

function _M.setClickAction(self,id,action)
if id==0 then
return
end
self.se.putString(id.."_click",action)
.apply()
end

-----------------

--样式 1黑 0白/跟系统 2透明
--使用不同布局，见resources
function _M.getStyle(self,id)
if id==0 then
return 0
end
return self.sp.getInt(id.."_style",0)
end

function _M.setStyle(self,id,s)
if id==0 then
return
end
self.se.putInt(id.."_style",s)
.apply()
end

--------------------

--应用于01样式
--[[背景
function _M.getAlpha(self,id)
return self.sp.getFloat(id.."_alpha",1)
end

function _M.setAlpha(self,id,a)
return self.se.putFloat(id.."_alpha",a)
.apply()
end]]

------------

function _M.getTextSize(self,id)
if id==0 then
return 18
end
return self.sp.getInt(id.."_text_size",18)
end

function _M.setTextSize(self,id,t)
if id==0 then
return
end
self.se.putInt(id.."_text_size",t)
.apply()
end

-----------------

function _M.getSourceUrl(self,id)
if id==0 then
return ""
end
return self.sp.getString(id.."_url","")
end

function _M.setSourceUrl(self,id,u)
if id==0 then
return
end
self.se.putString(id.."_url",u)
.apply()
end

----------------

--[[function _M.getAlignment(self,id)
return self.sp.getInt(id.."_gravity",17)
end

function _M.setAlignment(self,id,g)
self.se.putInt(id.."_gravity",g)
.apply()
end]]

---------------

function _M.getSentence(self,id,blank)
if id==0 then
return blank||""
end
local s=self.sp.getString(id.."_word","")
if s=="" then
return blank||s
end
return s
end

function _M.setSentence(self,id,s)
if id==0 then
return
end
self.se.putString(id.."_word",s)
.apply()
end

-------------------

function _M.getPendingSentence(self,id)
if id==0 then
return ""
end
return self.sp.getString(id.."_pending_word","")
end

function _M.setPendingSentence(self,id,s)
if id==0 then
return
end
self.se.putString(id.."_pending_word",s)
.apply()
end

function _M.isAutoDark()
return isAutoDark
end

return _M